<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import Edit from './components/Edit.vue';
import axios from 'axios'

// 列表渲染功能
let list = ref([])
const getList = async () => {
  const {data:res} = await axios.get('list')
  list.value = res.data
}
onMounted(() => getList())
//删除功能
const handleDelete = (async (index) => {
  await axios.delete('delete',{data:{id:`${index}`}})
  getList()
 
})
//编辑功能
const editRef = ref(null)
const handleEdit = (row) => {
  editRef.value.handleOpen(row)
}
</script>
<template>
  <div>
    <el-table :data="list" style="width: 60%" class="table">
      <el-table-column label="Id" prop="id" />
      <el-table-column label="Name" prop="name" />
      <el-table-column label="籍贯" prop="address" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" type="primary" @click="handleEdit(scope.row)">Edit</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <Edit ref="editRef"  @updateData="getList"/>
</template>

<style scoped>
.table {
  margin: auto;
}
</style>
